import store from '@/store'
import router from '@/router'

interface UserState {
    userId?: string,
    name?: string,
    token?: string,
    permissions: string[],
    rememberMe: boolean,
}

export const useUserStore = defineStore('user', {
    state: (): UserState => {
        return {
            userId: undefined,
            name: undefined,
            token: undefined,
            permissions: [],
            rememberMe: false,
        }
    },
    getters: {
        getUserId: (state) => {
            return state.userId
        },
        getName() : string | undefined {
            return this.name
        },
        getToken(state) {
            return state.token
        }
    },
    actions: {
        setUserId(userId: string) {
            this.userId = userId
        },
        setName(name: string) {
            this.name = name
        },
        setToken(token: string) {
            this.token = token
        },
        setPermissions(permissions: string[]) {
            this.permissions = permissions
        },
        setRememberMe(rememberMe: boolean) {
            this.rememberMe = rememberMe
        },
        logout() {
            this.userId = undefined
            this.name = undefined
            this.token = undefined
            this.permissions = []
            this.rememberMe = false

            router.push('/login')
        }
    },
    persist: true
})

export const useUserStoreWithOut = () => {
    return useUserStore(store)
}